"""
多线程提取 CNKI 文献信息，包括：
    - 期刊来源
    - 作者
    - 作者单位
    - 关键词
    - 摘要
    - 基金
    - DOI
    - 参考文献
"""

import logging
import threading
import time


from requests import RequestException

from config import SEARCH_QUERIES
from custom_exception import NoMorePagesException
from file_handler import FileHandler
from logger import setup_logger
from reference_scrapy import ReferencesScrapy


def start_scraping_task(search_query):
    """
    启动爬取任务
    """
    logger = setup_logger(search_query)

    restart_count = 0
    file_handler = FileHandler(search_query)
    thread_name = threading.current_thread().name
    logger.info(f"Thread {thread_name} started for search query: {search_query}")

    while True:
        try:
            ref_process = ReferencesScrapy(file_handler)
            logger.info(
                f"Thread {thread_name}: Starting scraping process for query: {search_query}"
            )

            total_start_time = time.time()
            ref_process.crawl_cnki_info_single(search_query)
            total_end_time = time.time()
            total_consume_time = int(total_end_time - total_start_time)

            logger.info(
                f"Thread {thread_name}: Total time consumed: {int(total_consume_time / 60)} minutes"
            )
            logger.info(
                f"Thread {thread_name}: Scraping process ended for query: {search_query}"
            )
            break
        # 处理不同异常
        except NoMorePagesException as e:
            logger.info(
                f"Thread {thread_name}: {e}. Successfully processed all documents, exiting."
            )
            ref_process.webdriver_handler.driver.quit()
            break

        except RequestException as e:
            logger.error(
                f"Thread {thread_name}_{search_query}: Network error encountered: {e}. Ending thread."
            )
            break

        except ConnectionResetError as e:
            logger.error(
                f"Thread {thread_name}_{search_query}: Connection error encountered: {e}. Ending thread."
            )
            break

        except Exception as e:
            restart_count += 1
            logger.error(
                f"{thread_name}: Encountered error: {e}. Restarting... Current restart count: {restart_count}"
            )
            # 如果重启次数超过10次，则终止线程
            if restart_count > 10:
                logger.error(
                    f"Max restart attempts reached. Terminating thread {thread_name}."
                )
                break
            continue


if __name__ == "__main__":
    """
    主函数
    """
    overall_start_time = time.time()

    threads = []
    for query in SEARCH_QUERIES:
        thread = threading.Thread(target=start_scraping_task, args=(query,))
        threads.append(thread)
        thread.start()

    for thread in threads:
        thread.join()

    overall_end_time = time.time()
    overall_consume_time = int(overall_end_time - overall_start_time)

    logging.info(
        f"Total time consumed for all threads: {int(overall_consume_time / 60)} minutes"
    )
